package com.hjxr.sysadmin.zsj.controller;

import java.util.List;
import java.util.Map;

import com.hjxr.common.constant.UserConstants;
import com.hjxr.sysadmin.zsj.domain.Inchainlocation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hjxr.framework.aspectj.lang.annotation.Log;
import com.hjxr.framework.aspectj.lang.enums.BusinessType;
import com.hjxr.sysadmin.zsj.domain.Industrialchain;
import com.hjxr.sysadmin.zsj.service.IIndustrialchainService;
import com.hjxr.sysadmin.web.controller.BaseController;
import com.hjxr.framework.web.domain.AjaxResult;
import com.hjxr.sysadmin.uitl.poi.ExcelUtil;
import com.hjxr.sysadmin.web.page.TableDataInfo;

/**
 * 产业链信息Controller
 *
 * @author zc
 * @date 2021-04-15
 */
@Api("产业链信息管理")
@RestController
@RequestMapping("/industrialchain")
public class IndustrialchainController extends BaseController
{
    @Autowired
    private IIndustrialchainService industrialchainService;

    /**
     * 查询产业链信息列表
     */
    @ApiOperation("查询产业链信息列表")
    @PreAuthorize("@ss.hasPermi('system:industrialchain:list')")
    @GetMapping("/list")
    public AjaxResult list(Industrialchain industrialchain)
    {
//        startPage();
        List<Industrialchain> list = industrialchainService.selectIndustrialchainList(industrialchain);
//        return getDataTable(list);
        return AjaxResult.success(list);
    }

    /**
     * 导出产业链信息列表
     */
    @ApiOperation("导出产业链信息列表")
    @PreAuthorize("@ss.hasPermi('system:industrialchain:export')")
    @Log(title = "导出产业链信息列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Industrialchain industrialchain)
    {
        List<Industrialchain> list = industrialchainService.selectIndustrialchainList(industrialchain);
        ExcelUtil<Industrialchain> util = new ExcelUtil<Industrialchain>(Industrialchain.class);
        return util.exportExcel(list, "industrialchain");
    }

    /**
     * 获取产业链信息详细信息
     */
    @ApiOperation("获取产业链信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:industrialchain:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(industrialchainService.selectIndustrialchainById(id));
    }

    /**
     * 新增产业链信息
     */
    @ApiOperation("新增产业链信息")
    @PreAuthorize("@ss.hasPermi('system:industrialchain:add')")
    @Log(title = "新增产业链信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Industrialchain industrialchain)
    {
        if (UserConstants.NOT_UNIQUE.equals(industrialchainService.checkInchainNameUnique(industrialchain)))
        {
            return AjaxResult.error("新增节点'" + industrialchain.getInchainSelect() + "'失败，节点名称已存在");
        }
        return toAjax(industrialchainService.insertIndustrialchain(industrialchain));
    }

    /**
     * 修改产业链信息
     */
    @ApiOperation("修改产业链信息")
    @PreAuthorize("@ss.hasPermi('system:industrialchain:edit')")
    @Log(title = "修改产业链信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Industrialchain industrialchain)
    {
        if (UserConstants.NOT_UNIQUE.equals(industrialchainService.checkInchainNameUnique(industrialchain)))
        {
            return AjaxResult.error("修改节点'" + industrialchain.getInchainSelect() + "'失败，节点名称已存在");
        }
        return toAjax(industrialchainService.updateIndustrialchain(industrialchain));
    }

    /**
     * 删除产业链信息
     */
    @ApiOperation("删除产业链信息")
    @PreAuthorize("@ss.hasPermi('system:industrialchain:remove')")
    @Log(title = "删除产业链信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        if (industrialchainService.hasChildByInchainId(id))
        {
            return AjaxResult.error("存在子节点,不允许删除");
        }
        return toAjax(industrialchainService.deleteIndustrialchainById(id));
    }

    /**
     * 是否存在产业链子节点
     */
    @ApiOperation("是否存在产业链子节点")
    @PreAuthorize("@ss.hasPermi('system:industrialchain:query')")
    @GetMapping("/hasChild/{id}")
    public AjaxResult hasChild(@PathVariable("id") Long id)
    {
        return AjaxResult.success(industrialchainService.hasChildByInchainId(id));
    }

    /**
     * 获取产业链主要区域信息
     */
    @ApiOperation("获取产业链主要区域信息")
    @GetMapping(value = "/industry/{inchainid}")
    public AjaxResult selectIndustryById(@PathVariable("inchainid") Long inchainid)
    {
        Inchainlocation inchainlocation = industrialchainService.selectIndustryById(inchainid);
        return AjaxResult.success(inchainlocation);
    }

    /**
     * 新增产业链主要区域信息
     */
    @ApiOperation("新增产业链主要区域信息")
    @Log(title = "新增产业链主要区域信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/addindustry")
    public AjaxResult insertIndustry(@RequestBody Inchainlocation inchainlocation)
    {
        return toAjax(industrialchainService.insertIndustry(inchainlocation));
    }

    /**
     * 修改产业链主要区域信息
     */
    @ApiOperation("修改产业链主要区域信息")
    @Log(title = "修改产业链主要区域信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editindustry")
    public AjaxResult updateIndustry(@RequestBody Inchainlocation inchainlocation)
    {
        return toAjax(industrialchainService.updateIndustry(inchainlocation));
    }
}
